package guiWindows;

import java.awt.Container;
import java.awt.GridBagLayout;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.JScrollPane;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.JTree;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import javax.swing.BoxLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.JSplitPane;

import objectTemplate.Template;

public class GUITemplatesSelector extends JDialog {

	private static final long serialVersionUID = 1L;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JPanel templatesPanel = null;
	private JPanel descriptionPanel = null;
	private JLabel descriptionLabel = null;
	private JPanel previewPanel = null;
	private JPanel centerPanel = null;
	private JPanel southPanel = null;
	private JPanel buttonPanel = null;
	private JSplitPane splitPanel = null;  //  @jve:decl-index=0:visual-constraint="641,280"
	private DefaultMutableTreeNode top;
	private DefaultTreeModel modelo;
	private JTree tree;
	private JPanel parametersPanel;
	private JLabel previewLabel = null;
	private JScrollPane jScrollPane = null;  //  @jve:decl-index=0:visual-constraint="10,1160"
	private LinkedList<Template> listTemplates;
	private LinkedList<TaskPanel> listPanelTask;
	
	public GUITemplatesSelector() {
		super();
		initialize();
	}

	private void initialize() {
		this.setSize(800, 500);
		this.setLayout(new BorderLayout());
		this.add(getSouthPanel(), BorderLayout.SOUTH);
		this.add(getSplitPanel(), BorderLayout.CENTER);
		this.setLocationRelativeTo(null);
		this.setModal(true);
		this.setTitle("Select Template");
		this.listPanelTask = new LinkedList<TaskPanel>();
	}

	public JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton("OK");
		}
		return okButton;
	}

	public JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton("CANCELAR");
		}
		return cancelButton;
	}

	public void setTreeSellectionListener(TreeSelectionListener tsl){
		this.tree.addTreeSelectionListener(tsl);
	}
	
	public void setListenerButtons(ActionListener al){
		this.okButton.addActionListener(al);
		this.cancelButton.addActionListener(al);
	}
	
	private JPanel getTemplatesPanel() {
		if(templatesPanel == null){
			top =  new DefaultMutableTreeNode("Templates");
			modelo = new DefaultTreeModel(top);
	        tree = new JTree(modelo);
	        tree.setRootVisible(false);
	        JScrollPane scrollPane = new JScrollPane(tree);
	        
	        templatesPanel = new JPanel();
	        templatesPanel.setSize(new Dimension(200, 200));
	        templatesPanel.add(scrollPane);
	        templatesPanel.setLayout(new BoxLayout(getTemplatesPanel(), BoxLayout.Y_AXIS));
	        templatesPanel.setBorder(BorderFactory.createTitledBorder(null, "Templates", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
	        scrollPane.setPreferredSize(templatesPanel.getSize());
	        scrollPane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
		}
		return templatesPanel;
	}
	
	public void loadTemplate(){
		for(int i=0;i<listTemplates.size();i++){
			Template currentTemplate = listTemplates.get(i);
			DefaultMutableTreeNode childTemplate = new DefaultMutableTreeNode(currentTemplate.getName());
			modelo.insertNodeInto(childTemplate, top, modelo.getChildCount(top));
		}
        tree.setRootVisible(true);
        tree.expandRow(0);
        tree.setRootVisible(false);
		this.setVisible(true);
	}

	public JTree getJTree() {
		return tree;
	}

	private JPanel getDescriptionPanel() {
		if (descriptionPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			descriptionPanel = new JPanel();
			descriptionPanel.setLayout(new GridBagLayout());
			descriptionPanel.setBorder(BorderFactory.createTitledBorder(null, "Description", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			descriptionPanel.add(getDescriptionLabel(centerPanel.getWidth()), gridBagConstraints1);
		}
		return descriptionPanel;
	}

	public JLabel getDescriptionLabel(int width) {
		if (descriptionLabel == null) {
			descriptionLabel = new JLabel();
			descriptionLabel.setHorizontalAlignment(JTextField.LEFT);
		}
		return descriptionLabel;
	}

	private JPanel getPreviewPanel() {
		if (previewPanel == null) {
			previewPanel = new JPanel();
			previewPanel.setLayout(new BorderLayout());
			previewPanel.setBorder(BorderFactory.createTitledBorder(null, "Preview", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			previewPanel.add(getPreviewLabel(), BorderLayout.CENTER);
		}
		return previewPanel;
	}

	public JPanel getCenterPanel() {
		if (centerPanel == null) {
			centerPanel = new JPanel();
			centerPanel.setLayout(new BorderLayout());
			centerPanel.add(getDescriptionPanel(), BorderLayout.NORTH);
			centerPanel.add(getPreviewPanel(), BorderLayout.CENTER);
		}
		return centerPanel;
	}

	private JPanel getParametersPanel() {
		if (parametersPanel == null) {
			parametersPanel = new JPanel();
			parametersPanel.setLayout(new BoxLayout(getParametersPanel(), BoxLayout.Y_AXIS));
			parametersPanel.setBorder(BorderFactory.createTitledBorder(null, "Parameters", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			parametersPanel.setVisible(false);
		}
		return parametersPanel;
	}
	
	private JPanel getSouthPanel() {
		if (southPanel == null) {
			southPanel = new JPanel();
			southPanel.setLayout(new BoxLayout(getSouthPanel(), BoxLayout.Y_AXIS));
			southPanel.add(getParametersPanel(), null);			
			southPanel.add(getButtonPanel(), null);
		}
		return southPanel;
	}

	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new FlowLayout());
			buttonPanel.add(getOkButton(), null);
			buttonPanel.add(getCancelButton(), null);
		}
		return buttonPanel;
	}

	private JSplitPane getSplitPanel() {
		if (splitPanel == null) {
			splitPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, getTemplatesPanel(), getJScrollPane());
			splitPanel.setSize(new Dimension(436, 117));
		}
		return splitPanel;
	}	
	
	public void loadTemplateInPanel(Template template){
		this.parametersPanel.setVisible(false);
		this.descriptionLabel.setText(template.getDescription());
		this.previewLabel.setIcon(template.getImage());
		for(int i=0;i<template.getNumberBranch();i++){
			TaskPanel taskPanel = new TaskPanel(i);
			listPanelTask.add(taskPanel);
		}
		this.parametersPanel.setVisible(true);
	}
	
	public LinkedList<TaskPanel> getListTaskPanel(){
		return listPanelTask;
	}
	
	public void setListenerTaskButtons(ActionListener al){
		for(int i=0;i<listPanelTask.size();i++){
			listPanelTask.get(i).getTaskButton().addActionListener(al);
			listPanelTask.get(i).getFluentButton().addActionListener(al);
		}
	}
	
	public void clearGUITemplate(){
		this.descriptionLabel.setText("");
		parametersPanel.removeAll();
	}

	public JLabel getPreviewLabel() {
		if (previewLabel == null) {
			previewLabel = new JLabel();
			previewLabel.setPreferredSize(new Dimension(373, 348));
		}
		return previewLabel;
	}

	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setSize(new Dimension(272, 25));
			jScrollPane.setViewportView(getCenterPanel());
		}
		return jScrollPane;
	}

	public void add(LinkedList<Template> mainGroupTemplate) {
		this.listTemplates = mainGroupTemplate;
	}
	
	public class TaskPanel extends JPanel{
		
		private static final long serialVersionUID = 1L;
		private FlowLayout flowLayout;
		private Box newPanelTask;
		private JTextField parameterField;
		private JButton button;
		private JButton buttonFluent;
		
		public TaskPanel(int i){
			flowLayout = new FlowLayout();
			flowLayout.setAlignment(java.awt.FlowLayout.LEFT);
			flowLayout.setHgap(1);
			parameterField = new JTextField();
			//parameterField.setEditable(false);
			parameterField.setSize(new Dimension(500,40));
			//parameterField.setMinimumSize(new Dimension(500,40));
			parameterField.setMaximumSize(new Dimension(500,40));
			button = new JButton("ASSIGN ACTIVITY "+(i+1));
			buttonFluent = new JButton("ASSIGN FLUENT");
			newPanelTask = Box.createHorizontalBox();
			//newPanelTask.setBorder(BorderFactory.createTitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
			//newPanelTask.setLayout(flowLayout);
			newPanelTask.add(parameterField, null);
			newPanelTask.add(button, null);
			newPanelTask.add(buttonFluent,null);
			parametersPanel.add(newPanelTask, null);
		}
		
		public JButton getTaskButton(){
			return button;
		}
		
		public JButton getFluentButton(){
			return buttonFluent;
		}
		
		public JTextField getTaskField(){
			return parameterField;
		}
	}
}
